import http from '../../apis/api.js';
import util from '../../utils/util.js';
const app = getApp();
let self;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pagination: {
      page: 1,
      limit: 8,
      more: 0
    },
    loading: false,
    orderList: [],
    ownNo: null,
    imageurl: '/images/img1.png'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    self = this;
    self.data.ownNo = options.ownNo;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    self.data.orderList = [];
    self.getOrderList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (self.data.pagination.more > 0) {
      self.data.pagination.page++;
      self.getOrderList();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getOrderList() {/**获取订单列表 */
    let postData = {
      ownNo: self.data.ownNo,
      limit: self.data.pagination.limit,
      page: self.data.pagination.page
    };
    self.setData({
      loading: true
    })
    http.queryAcctInPage(postData).then(res => {
      if (res.code == 0) {
        if (res.page.records.length > 0) {
          res.page.records.forEach((el, i) => {   
            el.formatStatus=''; 
            switch (el.inStat) {
              case "50600101":
                el.formatStatus = "待确认";
              break;
              case '50600102':
                el.formatStatus = "待支付确认"
                break;
              case '50600103':
                el.formatStatus = "待结算"
                break;
              case '50600104':
                el.formatStatus = "已结算"
                break;
            }
            el.formatTime = util.formatTime(el.inTime, 'YYYY-MM-DD hh:mm:ss');
            el.storePics.forEach(item=>{
              if (item.picType =='51000101'){
                el.thumbnail = item.inPath
              }
              
            })
            self.data.orderList.push(el);
          })
          self.data.pagination.more = Number(res.page.current) * Number(res.page.size) < res.page.total ? 1 : 0;
        }
        console.log(self.data.pagination.more);
        self.setData({
          orderList: self.data.orderList,
          pagination: self.data.pagination,
          loading: false
        })
      }
    })
  },
  submit(e){
    let detail = e.currentTarget.dataset.detail;
    if (detail.inStat =='50600101'){
      wx.showModal({
        title: '提示',
        content: '是否确认充值',
        success(res) {
          if (res.confirm) {
            wx.showLoading({
              title: '确认充值中，请稍后',
            })
            http.updateInStat(`?id=${detail.id}`).then(result=>{
              wx.hideLoading();
              if (result.code==0){
                wx.showToast({
                  title: '确认成功',
                  icon: 'success',
                  duration: 2000,
                  complete(){
                    self.onShow();
                  }
                })
              }
            })
          }
        }
      })
    }
    if (detail.inStat == '50600103') {
      wx.showModal({
        title: '提示',
        content: '是否确认收款',
        success(res) {
          if (res.confirm) {
            wx.showLoading({
              title: '确认收款中，请稍后',
            })
            http.updateInStat(`?id=${detail.id}`).then(result => {
              wx.hideLoading();
              if (result.code == 0) {
                wx.showToast({
                  title: '确认支付成功',
                  icon: 'success',
                  duration: 2000,
                  complete() {
                    self.onShow();
                  }
                })
              }
            })
          }
        }
      })
    }
  }
})